<?php
function includeFile($prefixPath,$name,$postfix='.php') {
	$_file = $prefixPath.$name.$postfix;
	if(is_file($_file)) {
		require_once $_file;
	} else {
        echo 'class: ' . $name . 'not find'; 
		$tmp = debug_backtrace();
		trigger_error(recursiveImplode("\n",$tmp),E_USER_WARNING);
		$name = htmlspecialchars($name);
		BaseModelCommon::debug($_file, 'The file is not exist');
		BaseModelMessage::showError('__autoload class file '.$name.' not found');
	}
}
//报错输出
function recursiveImplode( $glue, $pieces ) {
    foreach( $pieces as $r_pieces ) {
        if( is_array( $r_pieces ) ){
          	$retVal[] = recursiveImplode( $glue, $r_pieces );
        }
        else{
          	$retVal[] = $r_pieces;
        }
    }
    return @implode( $glue, $retVal );
}

function __autoload($name) {
	if(!preg_match('/^[A-Za-z_0-9]*$/', $name)) {
        header("Location: /");
		exit($name);
	}
    if($name == 'PEAR_Error') {
        //兼容SSO类
        return;
    }
	if( stripos($name, 'config') !== FALSE ) {
		includeFile(PATH_CONFIG, $name);
	} else if($name == 'FirePHP') {
		includeFile(PATH_LIBS_MODEL . "FirePHPCore/", $name, ".class.php");
	} else if(strpos($name, 'Smarty_') === 0) {
        $name = strtolower($name);
		includeFile(PATH_LIBS_VIEW . "sysplugins/", $name);
    } else if( stripos($name, 'smarty') !== FALSE ) {
		includeFile(PATH_LIBS_VIEW, $name,'.class.php');
	} else if($name == 'SSOClient') {
        include("SSOClient.php");
	} else if( stripos($name, 'base') === 0) {//base model
		 if( stripos($name, 'controller') !== FALSE ) {
			includeFile(PATH_LIBS_CTL, $name);
		 }
		 else if( stripos($name, 'model') !== FALSE ) {
		 	includeFile(PATH_LIBS_MODEL, $name);
		 }
		 else if( stripos($name, 'view') !== FALSE ) {
		 	includeFile(PATH_LIBS_VIEW, $name);
		 }else{}
	} else if( stripos($name, 'controller') !== FALSE ) {//bbs controller
		includeFile(PATH_APP_CTL, $name);
	} else{
		if( strlen(rtrim($name, 'DB')) === strlen($name) - 2) {
			includeFile(PATH_MODEL . 'db/', $name);
		}
		else {
			includeFile(PATH_MODEL, $name);
		}
	}
}
?>